﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Excellent
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }


        private void LoginForm_Load(object sender, EventArgs e)
        {
            string sql = string.Format(@"select *from T_Type");
            DataSet ds = DBHelper.GetDataSet(sql);
            this.cBtype.DataSource = ds.Tables[0];
            this.cBtype.DisplayMember = "typeName";
            this.cBtype.ValueMember = "typeId";
        }

        private void btLogin_Click(object sender, EventArgs e)
        {
            string nane = this.tBname.Text.Trim();
            string pwd = this.tBpassword.Text.Trim();
            string type = this.cBtype.Text.Trim();
            LoginType.UserName = nane;
            string sql;
            if (type == "管理员")
            {

                sql = string.Format("select*from Administrator where username='{0}' and password='{1}'", nane, pwd);

            }
            else
            {
                sql = string.Format("select*from StudentUsers where name='{0}' and password='{1}'", nane, pwd);
                TheuserForm d = new TheuserForm();
                d.Show();
                this.Hide();
                return;
            }

            DataSet ds = DBHelper.GetDataSet(sql);
            DataTable dt = ds.Tables[0];

            //判断是否登录成功
            if (dt.Rows.Count > 0)
            {
                LoginType.UserId = Convert.ToInt32(dt.Rows[0][0]);//用户Id
                LoginType.UserName = dt.Rows[0][1].ToString();
                LoginType.Type = type == "管理员" ? (int)Type.Admin : (int)Type.Student;

                AdministratorForm ad = new AdministratorForm();
                ad.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("账号或密码有误，请重新输入！");
            }


        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("你确定要关闭吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK)
            {
                //关闭
            }
            else
            {
                e.Cancel = true;//取消关闭
            }
        }
    }
}
